﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft Corp." file="DeleteHostedServiceOperation.cs">
//   All rights reserved. See License.txt in the project root for license information.
// </copyright>
// 
// --------------------------------------------------------------------------------------------------------------------
namespace CloudBlitz.DeploymentManager.DeploymentOperations
{
    using System;
    using System.Net;

    using AzureManagement.AzureDataContracts;

    using CloudBlitz.DeploymentManager.DeploymentWorkflow;

    public class DeleteHostedServiceOperation : DeploymentOperationBase
    {
        private readonly IAzureServiceClientsFactory azureServiceClientsFactory;

        public DeleteHostedServiceOperation(IAzureServiceClientsFactory azureServiceClientsFactory) : base(1)
        {
            this.azureServiceClientsFactory = azureServiceClientsFactory;
        }

        protected override OperationResult ExecuteOverride(DeploymentContext context)
        {
            var azureServiceManagementClient = this.azureServiceClientsFactory.CreateServiceManagementClient(context.AzureCredentials);

            try
            {
                this.RaiseProgressChangedEvent(0, string.Format("Deleting hosted service \"{0}\"", context.HostedServiceName));

                var operationId = azureServiceManagementClient.DeleteHostedService(context.HostedServiceName).Result;

                this.RaiseProgressChangedEvent(50, string.Format("Hosted service \"{0}\" is being deleted, waiting for the operation to complete", context.HostedServiceName));
                var operation = azureServiceManagementClient.WaitOperationForCompletion(operationId, TimeSpan.MaxValue);

                if (operation.Status != OperationStatuses.Succeeded)
                {
                    return new OperationResult(DeploymentOperationStatus.Failed, operation.Error.Message);
                }
            }
            catch (AggregateException e)
            {
                if (e.InnerException is WebException)
                {
                    var webException = e.InnerException as WebException;
                    var httpWebResponse = webException.Response as HttpWebResponse;
                    if (httpWebResponse != null && httpWebResponse.StatusCode == HttpStatusCode.NotFound)
                    {
                        this.RaiseProgressChangedEvent(100, string.Format("Hosted service \"{0}\" doesn't exist", context.HostedServiceName));
                        return OperationResult.Success;
                    }
                }

                return ExceptionHelper.HandleException(e);
            }
            catch (Exception e)
            {
                return ExceptionHelper.HandleException(e);
            }

            this.RaiseProgressChangedEvent(100, string.Format("Hosted service \"{0}\" has been deleted", context.HostedServiceName));
            return OperationResult.Success;
        }
    }
}